package com.kobeliu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kobeliu.base.BaseInfoProperties;
import com.kobeliu.exceptions.GraceException;
import com.kobeliu.mapper.AdminMapper;
import com.kobeliu.pojo.Admin;
import com.kobeliu.pojo.bo.AdminBO;
import com.kobeliu.pojo.bo.AdminInfoBO;
import com.kobeliu.pojo.vo.AdminVO;
import com.kobeliu.result.GraceJSONResult;
import com.kobeliu.result.ResponseStatusEnum;
import com.kobeliu.service.AdminService;
import com.kobeliu.utils.MD5Utils;
import com.kobeliu.utils.PagedGridResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.bouncycastle.jcajce.provider.digest.MD5;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Controller
public class AdminServiceImpl extends BaseInfoProperties implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Boolean adminLogin(AdminBO adminBO) {

        Admin admin = adminMapper.selectOne(new QueryWrapper<Admin>().eq("username", adminBO.getUsername()));

        if(admin!=null){
            //获取盐salt
            String slat = admin.getSlat();
            if(StringUtils.isNotBlank(slat)){
                //将传过来的password与盐进行MD5 之后与 存在db中的password做对比
                String encrypt = MD5Utils.encrypt(adminBO.getPassword(), slat);
                if(admin.getPassword().equalsIgnoreCase(encrypt)){
                    return true;
                }
            }
        }


        return false;
    }

    @Override
    public AdminVO getAdminVO(AdminBO adminBO) {
        Admin admin = adminMapper.selectOne(new QueryWrapper<Admin>().eq("username", adminBO.getUsername()));

        if(admin!=null){
            AdminVO adminVO = new AdminVO();
            BeanUtils.copyProperties(admin,adminVO);
            return adminVO;
        }

        return null;
    }

    /**
     * 创建新用户
     * @param adminInfoBO
     */
    @Transactional
    @Override
    public void createAdmin(AdminInfoBO adminInfoBO) {

        Admin admin = adminMapper.selectOne(new QueryWrapper<Admin>().eq("username", adminInfoBO.getUsername()));

        if(admin != null){
            //代表对象已存在
            //优雅解耦
            GraceException.display(ResponseStatusEnum.ADMIN_USERNAME_EXIST_ERROR);
        }

        //创建admin
        Admin newAdmin = new Admin();
        BeanUtils.copyProperties(adminInfoBO,newAdmin);

        //生成盐salt
        String code = (int)((Math.random()*9+1)*100000) + "";

        //获得MD5加密后的值
        String pwd = MD5Utils.encrypt(adminInfoBO.getPassword(),code);

        newAdmin.setPassword(pwd);
        newAdmin.setSlat(code);

        newAdmin.setCreateTime(LocalDateTime.now());
        newAdmin.setUpdatedTime(LocalDateTime.now());

        adminMapper.insert(newAdmin);
    }

    /**
     * 分页查询admin列表
     * @param accountName
     * @param page
     * @param limit
     * @return
     */
    @Override
    public PagedGridResult getAdminList(String accountName, Integer page, Integer limit) {

        PageHelper.startPage(page, limit);
        List<Admin> admins = adminMapper.selectList(new QueryWrapper<Admin>().like("username",accountName));

        return setterPagedGrid(admins,page);
    }

    @Override
    public void deleteAdmin(String username) {

        Integer sum = adminMapper.delete(new QueryWrapper<Admin>()
                .eq("username",username)
                .ne("username","admin"));

        if(sum == 0){
            //没有任何数据被改变
            GraceException.display(ResponseStatusEnum.ADMIN_DELETE_ERROR);
        }
    }


}
